package com.zy.openchat.core.model.message;


import com.zy.openchat.core.model.user.UserInfo;
import com.zy.openchat.util.Common;

import java.util.List;

public class UiMessage {
    public boolean isPlaying;
    public boolean isDownloading;
    public boolean isFocus;
    public boolean isChecked;
    public boolean isAvaterDownloaded;
    public int currentPosition;//当前播放进度
    public int secendProssed;//缓冲进度
    public int totalLenght;//音频总时长
    /**
     * media类型消息，上传或下载的进度
     */
    public int progress;

    public int playingReactionId = 0;

    public List<MessageReaction> reactions;

    public boolean isRequestMessageReactions = false;

    public MessageInfo message;

    public String translationText;

    public String transferVocieText;

    public UiMessage(MessageInfo message) {
        this.message = message;
    }

    public MessageReaction haveReactionByCurrentUserId(){
        if(UserInfo.getUserInfo()!=null){
            return haveReactionByUserId(UserInfo.getUserInfo().id);
        }
        return null;
    }

    public MessageReaction haveReactionByUserId(long userId){
        if(Common.isEmpty(reactions)){
            return null;
        }
        for(MessageReaction reaction:reactions){
            if(!Common.isEmpty(reaction.userIds)){
                for(Long innerUserId:reaction.userIds){
                    if(innerUserId == userId){
                        return reaction;
                    }
                }
            }
        }
        return null;
    }


    public boolean isMessageEqual(UiMessage uiMessage){
        if(uiMessage==null||uiMessage.message==null||message==null){
            return false;
        }
        return message.id == uiMessage.message.id;
    }

    public boolean isAvaterDownloadEqual(UiMessage uiMessage){
        if(uiMessage==null||uiMessage.message==null||message==null){
            return false;
        }
        UserInfo oldUserInfo = UserInfo.getUserInfo(message.sender_user_id());
        UserInfo newUserInfo = UserInfo.getUserInfo(uiMessage.message.sender_user_id());
        if(oldUserInfo==null&&newUserInfo==null){
            return true;
        }
        if(oldUserInfo==null||newUserInfo==null){
            return false;
        }
        boolean newIsAvaterDownloaded = false;
        if(newUserInfo.profile_photo!=null&&!newUserInfo.profile_photo.isSmallPhotoDownloaded()){
            newIsAvaterDownloaded = true;
        }
        return newIsAvaterDownloaded == isAvaterDownloaded;
    }

}
